import { Http } from '../Utils/Api'
import { themeDefaultData } from '../Config/default'
import { Encrypt } from '../Utils/IndexedDB'
import { Button } from 'antd'
import { v4 } from 'uuid'

export const getBaseData = async (notification) => {
    try {
        return await Http({ method: 'get', url: 'dictionary/GetJsonInfoByUuid', params: { uuid: `THEME/THEMEJSON` } })
    } catch (err) {
        notification.open({
            key: 'initTheme',
            duration: 0,
            closeIcon: false,
            message: '文件获取失败',
            description: '未获取到项目初始化文件.请联系管理员或刷新页面!',
            actions: (
                <>
                    <Button type="link" onClick={() => window.location.reload()}>重新加载</Button>
                    {window.location.port && <Button type='primary' onClick={onRegenerate}>生成新配置文件</Button>}
                </>
            )
        })
    }
}

export const onRegenerate = async () => {
    const params = { uuid: 'THEME/THEMEJSON', content: JSON.stringify(await Encrypt(themeDefaultData)) }
    await Http({ method: 'post', url: 'dictionary/SaveJsonInfoByUuid', params })
    window.location.reload()
}